package com.orkasgb.framework.springsecurity.usernamepassword;

import com.orkasgb.framework.springsecurity.OrkasgbJWTSecurityContextRepository;
import com.orkasgb.framework.orkasgbframework.spring.springsecurity.sms.OrkasgbSMSSecurityConfig;
import com.orkasgb.framework.orkasgbframework.spring.springsecurity.sms.OrkasgbSMSUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

/**
 * spring security自动化配置。
 */
@Configuration
@EnableAutoConfiguration
@EnableMethodSecurity
@EnableWebSecurity
public class OrkasgbSecurityConfig {

    @Autowired
    private OrkasgbUserDetailsService orkasgbUserDetailsService;

    @Autowired
    private OrkasgbSuccessHandle orkasgbSuccessHandle;

    @Autowired
    private OrkasgbFailureHandler orkasgbFailureHandler;

    @Autowired
    private OrkasgbJWTSecurityContextRepository orkasgbJWTSecurityContextRepository;

    @Autowired
    private OrkasgbSMSUserDetailsService orkasgbSMSUserDetailsService;

    /**
     * 配置 SecurityFilterChain
     *
     * @param httpSecurity HttpSecurity实例
     * @return HttpSecurity实例
     * @throws Exception 异常
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 配置表单登录
                .formLogin()
                // 配置处理登录请求的URL
                .loginProcessingUrl( "/login")
                // 配置请求参数，获取用户名username
                .usernameParameter("username")
                // 配置请求参数，获取密码password
                .passwordParameter("password")
                // 配置处理成功的回调
                .successHandler(orkasgbSuccessHandle)
                // 配置处理失败的回调
                .failureHandler(orkasgbFailureHandler)
                .securityContextRepository(orkasgbJWTSecurityContextRepository)
                .and()
                .securityContext()
                .securityContextRepository(orkasgbJWTSecurityContextRepository)
                .and()
                .cors()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 配置自定义UserDetailsService
                .userDetailsService(orkasgbUserDetailsService)
                // 禁止csrf跨站请求伪造
                .csrf()
                .disable()
                // 所有的请求都需求要认证
                .authorizeHttpRequests()
                .requestMatchers("/login", "/loginOut", "/excelTest/*")
                .permitAll()
                // 所有的请求都需求要认证
                .anyRequest()
                .permitAll()
                // 所有的请求都需求要认证
                // .authenticated()
                .and()
                // 异常处理器
                .exceptionHandling();
                //.authenticationEntryPoint(authenticationEntryPoint);


        OrkasgbSMSSecurityConfig<HttpSecurity> orkasgbSMSSecurityConfig =
                new OrkasgbSMSSecurityConfig<>();
        orkasgbSMSSecurityConfig.securityContextRepository(orkasgbJWTSecurityContextRepository);
        orkasgbSMSSecurityConfig.loginProcessingUrl("/codeLogin").permitAll();
        httpSecurity.apply(orkasgbSMSSecurityConfig);
        return httpSecurity.build();
    }

}
